<?php

$mcrp = './';
include_once('functions/session.php');
include_once('functions/users.php');
include_once('skin/default.php');

if(isset($_GET['mail']) && isset($_GET['mailc']))
{
	if($_GET['mail'] != $_GET['mailc'])
		$Error = '0Les adresses email ne correspondent pas.';
	else if(!preg_match('#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#', $_GET['mail']))
		$Error = '0Adresse email invalide.';
	else
	{
		$mcuser->email = $_GET['mail'];
		UpdateUser($mcuser);
		$Error = '1Adresse email mise à jour.';
	}
	echo $Error;
	exit;
}

if(isset($_GET['ppass']) && isset($_GET['pass']) && isset($_GET['passc']))
{
	if($_GET['pass'] != $_GET['passc'])
		$Error = '0Les mots de passe ne correspondent pas.';
	else if($mcuser->password != $_GET['ppass'])
		$Error = '0Mot de passe incorrect.';
	else
	{
		$mcuser->password = $_GET['pass'];
		UpdateUser($mcuser);
		$temp = $mcuser->name;
		$db->query('UPDATE ' . $db->prefix . 'users SET password=\'' . $_GET['pass'] . '\' WHERE id=' . $mcuser->id);
		LogoutUser();
		LoginUser($temp, $_GET['pass']);
		$Error = '1Mot de passe mis à jour.';
	}
	echo $Error;
	exit;
}

if(isset($_GET['u']))
{
	$profil = GetUser($_GET['u']);
	if($profil === false) { header('Location: rank.php'); exit; }
}
else if(!isset($_GET['id']) && $mcuser->id != 0)
{
	$profil = $mcuser;
}
else if(!isset($_GET['id']))
{
	header('Location: rank.php');
	exit;
}
else
{
	$profil = GetUserById($_GET['id']);
	if($profil === false) { header('Location: rank.php'); exit; }
}

$SkinTitle = 'µContest - ' . $profil->name;
	
include('skin/header.php');

?>

<div class="mc_box">	
	<h3><a href="rank.php">Profil</a> :: <?php echo $profil->name . ($profil->isadmin?'<span style="padding-left:10px">[admin]</span>':'') . ($mcuser->isadmin?('<a href="admin/users.php?id=' . $profil->id . '"><span style="padding-left:10px; font-size:.6em;">Edit</a></a>'):''); ?></h3>
	<div class="mc_box_content">
		<h3>Informations<?php if($mcuser->isadmin && !$profil->isadmin) { ?> - <a href="log/user/<?php echo $profil->id; ?>.log" style="font-size:.9em;">Log navigation</a><?php } ?></h3>
		<table id="mc_profil_infos">
			<tr>
				<td class="pt1"><b>Pseudo : </b><?php echo $profil->name; ?></td>
				<td class="pt2"><b>Epreuves validées : </b><?php echo count($profil->contestcompleted); ?></td>
			<tr>
			<tr>
				<td class="pt1"><b>Points : </b><?php echo $profil->points; ?></td>
				<td class="pt2"><b>Dernier tentative : </b><?php if($profil->lasttry != 0) echo date('d/m/Y - H:i:s', $profil->lasttry); ?></td>
			</tr>
			<tr>
				<td class="pt1"><b>Rang : </b><?php echo $profil->rank; ?></td>
				<td class="pt2"><b>Dernière épreuve validée : </b><?php $c = GetContestById(end($profil->contestcompleted)); echo '<a href="contest.php?id=' . $c->id . '">' . stripslashes($c->title) . '</a>'; ?></td>
			</tr>
			<?php if($mcuser->id == $profil->id || $mcuser->isadmin == 1) { ?>
			<tr>
				<td class="pt1"><b>Adresse email : </b><span id="mc_profil_email"><?php echo $profil->email; ?></span> (<a href="#" onclick="ToggleDisplay('mc_profil_new_mail');return false;">Modifier</a>)</td>
				<td class="pt2"><b>Mot de passe : </b><a href="#" onclick="ToggleDisplay('mc_profil_new_pass');return false;">Modifier</a>
			</tr>
			<tr>
				<td class="pt1">
					<table id="mc_profil_new_mail" style="display: none;">
						<tr>
							<td><b>Nouvelle adresse : </b></td>
							<td><input id="new_mail"></input></td>
						</tr>
						<tr>
							<td><b>Confirmation : </b></td>
							<td><input id="new_mail_confirm"></input></td>
						</tr>
						<tr>
							<td colspan=2 style="text-align:center;"><button onclick="ToggleDisplay('mc_profil_new_mail');" style="margin-right:7px;">Annuler</button><button onclick="NewMail();">Valider</button></td>
						</tr>
						<tr>
							<td colspan=2><span id="mc_mail_error"></span></td>
						</tr>
					</table>
				</td>
				<td class="pt2">
					<table id="mc_profil_new_pass" style="display: none;">
						<tr>
							<td><b>Ancien mot de passe : </b></td>
							<td><input id="prev_pass" type="password"></input></td>
						</tr>
						<tr>
							<td><b>Nouveau mot de passe : </b></td>
							<td><input id="new_pass" type="password"></input></td>
						</tr>
						<tr>
							<td><b>Confirmation : </b></td>
							<td><input id="new_pass_confirm" type="password"></input></td>
						</tr>
						<tr>
							<td colspan=2 style="text-align:center;"><button onclick="ToggleDisplay('mc_profil_new_pass');" style="margin-right:7px;">Annuler</button><button onclick="NewPass();">Valider</button></td>
						</tr>
						<tr>
							<td colspan=2><span id="mc_pass_error"></span></td>
						</tr>
					</table>
				</td>
			</tr>
			<?php } ?>
		</table>
		<br />
		<h3>Palmarès</h3>
		<?php DisplayCacheContent('include/profilcontestslist.php', 60, '', $profil->id); ?>
	</div>
</div>	

<?php

include('skin/footer.php');

?>